﻿/*$(document).ready(function() {
	$("#ContentArray").dialog({
		autoOpen : false,
		module : true,
		width : 380,
		buttons : {
			确定 : function() {
				getOrderContent();
			}
		},
		overlay : {
			opacity : 0.5,
			background : "black",
			overflow : 'auto'
		},
		modal : true,
		hide : "fadeout",
		module : true
	});
});*/
var f = new Array();
function changChecked() {
	var obj = document.getElementById("selectDefault");
	if (obj.checked) {
		// 选中的动作
		if ($("#showusersNamea1").html() == ""
				|| $("#showusersNamea1").html() == 'null') {
			$("#login").dialog("open");
			obj.checked = false;
		} else {
			$.getJSON("getSessionCustomer.action",function(data) {
				document.getElementById("address").value = data.user.userAddress==null?"":data.user.userAddress;
				document.getElementById("telephone").value = data.user.userMobilePhone==null?"":data.user.userMobilePhone;
				document.getElementById("username").value = data.user.userRealName==null?"":data.user.userRealName;
				setArea(data.user.userProvince,data.user.userCity, data.user.userArea);
				var phone1 = data.user.usertelePhone;
				if (phone1 != "-" && phone1 != "" && phone1 != 'null' && phone1 != null) {
					document.getElementById("specialplane1").value = phone1.split("-")[0];
					document.getElementById("specialplane2").value = phone1.split("-")[1];
				}
				document.getElementById("username").style.color = "#000000";
				setArea(data.user.userProvince,data.user.userCity,data.user.userArea);
			});
		}
	} else {
		// 取消选中的动作
		document.getElementById("address").value = "";
		document.getElementById("telephone").value = "";
		document.getElementById("username").value = "写收件人真实姓名";
		document.getElementById("username").style.color = "#8a8a8a";
	}
}
function checkAll() {
	var obj = document.getElementById("checkAllbox");
	var cb1 = document.getElementsByName("checkbox1");
	if (obj.checked) {
		for ( var i = 0; i < cb1.length; i++) {
			cb1[i].checked = true;
		}
	} else {
		for ( var i = 0; i < cb1.length; i++) {
			cb1[i].checked = false;
		}
	}
	refreshTotalPrice();
	refreshTradeName();
}
function usernameFocus() {
	var tele = document.getElementById("username");
	if (tele.value == "写收件人真实姓名") {
		tele.value = "";
	}
	tele.style.color = "#000000";
}
function checkUsername() {
	var tele = document.getElementById("username").value;
	if (tele != null && tele != "写收件人真实姓名" && tele != "") {
		document.getElementById("usernameMsg").innerHTML = "√";
		f[1] = 1;
	} else {
		document.getElementById("usernameMsg").innerHTML = "×";
		document.getElementById("username").style.color = "#8a8a8a";
		document.getElementById("username").value = "写收件人真实姓名";
	}
}
function checkAddress() {
	var tele = document.getElementById("address").value;
	if (tele != null) {
		document.getElementById("addressMsg").innerHTML = "√";
		f[2] = 1;
	} else {
		document.getElementById("addressMsg").innerHTML = "×";
	}
}
function checkMobile() {
	var tele = document.getElementById("telephone").value;
	if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(tele))) {
		document.getElementById("telephoneMsg").innerHTML = "×";
		f[3] = 0;
	} else {
		document.getElementById("telephoneMsg").innerHTML = "√";
		f[3] = 1;
	}
}
function checkPhone() {
	var tele1 = document.getElementById("specialplane1").value;
	var tele2 = document.getElementById("specialplane2").value;
	if (!(/^\d{2,4}-\d{8}$/.test(tele1 + "-" + tele2))) {
		document.getElementById("specialplaneMsg").innerHTML = "×";
		f[4] = 0;
	} else {
		document.getElementById("specialplaneMsg").innerHTML = "√";
		f[4] = 1;
	}
}
function mailFocus() {
	var tele = document.getElementById("mail");
	if (tele.value == "写6位邮政编码") {
		tele.value = "";
	}
	tele.style.color = "#000000";
}
function checkMail() {
	var tele = document.getElementById("mail").value;
	if (!(/^[1-9][0-9]{5}$/.test(tele))) {
		document.getElementById("mailMsg").innerHTML = "×";
		document.getElementById("mail").style.color = "#8a8a8a";
		document.getElementById("mail").value = "写6位邮政编码";
	} else {
		document.getElementById("mailMsg").innerHTML = "√";
		f[5] = 1;
	}
}
var readyToBuy = 0;
/*
 * function checkAllInput() { checkUsername(); checkAddress(); checkMobile();
 * checkPhone(); checkMail(); if (f[1] == f[2] == f[3] == f[5] == 1) {
 * document.getElementById("username").disabled = true;
 * document.getElementById("address").disabled = true;
 * document.getElementById("telephone").disabled = true;
 * document.getElementById("mail").disabled = true;
 * document.getElementById("selProvance").disabled = true;
 * document.getElementById("selCity").disabled = true;
 * document.getElementById("selArea").disabled = true;
 * document.getElementById("makeSure1").style.display = "none";
 * document.getElementById("makeSure2").style.display = ""; readyToBuy = 1; if
 * (f[4] == 1) { document.getElementById("specialplane1").disabled = true;
 * document.getElementById("specialplane2").disabled = true; } } else {
 * alert("请填写完整、正确"); } }
 */

function showGetOrderContent() {
	if ($("#showusersNamea1").html() == "" || $("#showusersNamea1").html() == 'null') {
		$("#login").dialog("open");
	} else {
		if (isFullForm()) {
			//window.location.replace(this.href);
			//window.event.returnValue=false;
			var id_num = new Array();
			var checkbox = document.getElementsByName("checkbox1");
			for ( var i = 0; i < checkbox.length; i++) {
				if (checkbox[i].checked) {
					id_num[i] = checkbox[i].value + ","
							+ document.getElementById(i + ",buyCount").value;
				}
			}
			window.location.href = "updateCustomAtShop.action?receiveName="
					+ $("#username").val() + "&&receiveProvince="
					+ $("#selProvance").val() + "&&receiveCity="
					+ $("#selCity").val() + "&&area=" + $("#selArea").val()
					+ "&&address=" + $("#address").val() + "&&postCode="
					+ $("#mail").val() + "&&phone1="
					+ $("#specialplane1").val() + "&&phone2="
					+ $("#specialplane2").val() + "&&mobilePhone="
					+ $("#telephone").val() + "&&id_num=" + id_num+"&&remark="+$("#remark").val();
		}
	}
}
function directPay(){//购物车支付宝接口
	if(isGoodsSelect()){
		if(isFullForm()){
			document.getElementsByName("alipayment")[0].submit();
			var id_num = new Array();
			var checkbox = document.getElementsByName("checkbox1");
			for ( var i = 0; i < checkbox.length; i++) {
				if (checkbox[i].checked) {
					id_num[i] = checkbox[i].value +","+ document.getElementById(i + ",buyCount").value;
				}
			}
			$.ajax({
				url:"updateCustomAtShop_ajax.action",
				data:{
					receiveName:$("#username").val(),
					receiveProvince:$("#selProvance").val(),
					receiveCity:$("#selCity").val(),
					address:$("#address").val(),
					postCode:$("#mail").val(),
					phone1:$("#specialplane1").val(),
					phone2:$("#specialplane2").val(),
					mobilePhone:$("#telephone").val(),
					id_num:id_num.toString(),
					tid:$("[name='hiddentid']").val()
				}
			});
		}
	}

}

function directPayFor(){//直接购买页面的付款
	if(isFullForm()){
		document.getElementsByName("alipayment")[0].submit();
		var id_num = new Array();
		for ( var i = 0; i < $("[name='hiddenCustomid']").size(); i++) {
			id_num[i] = $("[name='hiddenCustomid']").eq(i).val() +","+ document.getElementById(i + ",buyCount").value;
		}
		$.ajax({
			url:"createCustomAtShop.action",
			data:{
				receiveName:$("#username").val(),
				receiveProvince:$("#selProvance").val(),
				receiveCity:$("#selCity").val(),
				receiveArea:$("#selArea").val(),
				address:$("#address").val(),
				postCode:$("#mail").val(),
				phone1:$("#specialplane1").val(),
				phone2:$("#specialplane2").val(),
				mobilePhone:$("#telephone").val(),
				id_num:id_num.toString(),
				tid:$("[name='hiddentid']").eq(0).val(),
				filename:$("[name='hiddenpicture']").eq(0).val(),
				pi:$("[name='hiddendetail']").eq(0).val(),
				remark:$("#remark").val()
			}
		});
	}
}

function isGoodsSelect(){
	var checkedMeg = document.getElementsByName("checkbox1");
	for ( var rr = 0; rr < checkedMeg.length; rr++){
		if(checkedMeg[rr].checked){
			return true;
		}
	}
	//alert("请选择商品");
	$.weeboxs.open('请选择商品!', {
		title:'提示',
		timeout:5,
		showCancel:false,
		okBtnName: '确定',
	});
	return false;
}

function isFullForm(){
	checkUsername();
	checkAddress();
	checkMobile();
	checkPhone();
	//checkMail();
	if (f[1]==1&&f[2]==1&&(f[3]+f[4]>0)) {
		return true;
	} else {
		//alert("请填写完整");
		$.weeboxs.open('请填写完整!', {
			title:'提示',
			timeout:5,
			showCancel:false,
			okBtnName: '确定',
		});
		return false;
	}
}

/*
 * function backToCheck() { document.getElementById("username").disabled =
 * false; document.getElementById("address").disabled = false;
 * document.getElementById("telephone").disabled = false;
 * document.getElementById("specialplane1").disabled = false;
 * document.getElementById("specialplane2").disabled = false;
 * document.getElementById("mail").disabled = false;
 * document.getElementById("selProvance").disabled = false;
 * document.getElementById("selCity").disabled = false;
 * document.getElementById("selArea").disabled = false; readyToBuy = 0;
 * document.getElementById("makeSure1").style.display = "";
 * document.getElementById("makeSure2").style.display = "none"; }
 */
function checkNumber(id) {
	var number = document.getElementById(id + ",buyCount").value;
	if (!(/^\d{1,}$/.test(number))) {
		//alert("请输入数字!");
		$.weeboxs.open('请输入数字!', {
			title:'提示',
			timeout:5,
			showCancel:false,
			okBtnName: '确定',
		});
	} else {
		var price = $("#" + id + "priceid").val();
		var p = price * number;
		$("#" + id + "jiageid").html(p + ".0元");
		refreshTotalPrice();
	}
}

function checkNumber1(id) {
	var number = document.getElementById(id + ",buyCount").value;
	var pri=0;
	var checkedMeg = ($("#goodsList tbody").children("tr").size())/3;
	if (!(/^\d{1,}$/.test(number))) {
		//alert("请输入数字!");
		$.weeboxs.open('请输入数字!', {
			title:'提示',
			timeout:5,
			showCancel:false,
			okBtnName: '确定',
		});
	} else {
		var price = $("#" + id + "priceid").val();
		var p = price * number;
		$("#" + id + "jiageid").html(p + ".0元");
		for ( var rr = 0; rr < checkedMeg; rr++){
			var a=$("#"+rr+"jiageid").html();
			pri=pri+parseFloat(a.substr(0,a.length-1));
		}
		$("input[name='totalprice']").val(pri);
	}
}

function refreshTotalPrice(){
	//重置totalprice（name）的value=所有打勾的price的总和
	var pri=0;
	var checkedMeg = document.getElementsByName("checkbox1");
	for ( var rr = 0; rr < checkedMeg.length; rr++){
		if(checkedMeg[rr].checked){
			var a=$("#"+rr+"jiageid").html();
			//alert(a);
			pri=pri+parseFloat(a.substr(0,a.length-1));
			//alert(pri);
		}
	}
	$("input[name='totalprice']").val(pri);
}
function refreshTradeName(){
	//重置hiddentname（name）的value=所有打勾的商品
	var pri='';var flag=1;
	var checkedMeg = document.getElementsByName("checkbox1");
	for ( var rr = 0; rr < checkedMeg.length; rr++){
		if(checkedMeg[rr].checked){
			if(flag!=1) pri+=",";
			pri+=$("body #IDmsg:eq("+rr+")").val();
			flag=0;
		}
	}
	$("input[name='hiddentname']").val(pri);
}
/*
 * function getOrderContent() { content_bak =
 * document.getElementsByName("ordercontent");
 * document.getElementById("updateCustomForm").submit(); //saveOrder(); }
 */
/*
 * function saveOrder() { 多条订单的公共信息 var username =
 * document.getElementById("username").value;// 收货人的真实姓名 var address =
 * document.getElementById("address").value; var mail =
 * document.getElementById("mail").value; var specialplane =
 * document.getElementById("specialplane1").value + "-" +
 * document.getElementById("specialplane2").value; var telephone =
 * document.getElementById("telephone").value; var province =
 * document.getElementById("selProvance").value.substring(6); var city =
 * document.getElementById("selCity").value.substring(6); var area =
 * document.getElementById("selArea").value; var detail =
 * document.getElementById("hiddendetail" + 0).value; var shoppicture =
 * document.getElementById("shoppingcarpicture").src; var theme =
 * document.getElementById("orderthemearray").value;
 *
 * 单条信息
 *
 * var checkedMeg = document.getElementsByName("checkbox1");// 先获得多选框信息 var
 * IDmsg_bak = document.getElementsByName("IDmsg");// ID信息，第一位是0：Custom //
 * 1：Order var number_bak = document.getElementsByName("buyCount");// 购买数量
 *
 * var IDmsg = new Array(); var number = new Array(); var content = new Array();
 *
 * var flagAtShopp = 0; for ( var rr = 0; rr < checkedMeg.length; rr++) { if
 * (checkedMeg[rr].checked) { IDmsg[flagAtShopp] = IDmsg_bak[rr].value;
 * number[flagAtShopp] = number_bak[rr].value; content[flagAtShopp] =
 * content_bak[rr].value; flagAtShopp++; } } window.location.href =
 * "updateCustomAtShop.action?customIdArr=" + IDmsg + "&numberArr=" + number +
 * "&username=" + username + "&address=" + address + "&mail=" + mail +
 * "&specialplane=" + specialplane + "&telephone=" + telephone + "&province=" +
 * province + "&city=" + city + "&area=" + area + "&contentArray=" + content +
 * "&ShoppingCarDetail=" + detail + "&shoppicture=" + shoppicture +
 * "&shoptheme=" + theme; }
 */
function reDesignAtShoppingCar(id) {
	window.location.href = "showCustom.action?customId=" + id;
	/*
	 * var detail = document.getElementById("hiddendetail" + id).value;
	 * customcontent = document.getElementById("contentAtshop").value;
	 * customname = document.getElementById("orderthemearray").value;
	 * window.location.href = "showCustom.action?ShoppingCarDetail=" + detail +
	 * "&customcontent=" + customcontent + "&customname=" + customname;
	 */
}

